import {
    Injectable,
    NestInterceptor,
    CallHandler,
    ExecutionContext,
} from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
interface Response<T> {
    data: T;
}
@Injectable()
export class TransformInterceptor<T>
    implements NestInterceptor<T, Response<T>> {
    intercept(
        context: ExecutionContext,
        next: CallHandler<any>,
    ): Observable<Response<T>> {
        context
        return next.handle().pipe(
            map(res => {
                const code = res && res.code ? res.code : 0
                const msg = res && res.msg ? res.msg : '请求成功';
                const data = res && res.data ? res.data : {};
                return {
                    data,
                    code: code,
                    message: msg,
                };
            }),
        );
    }
}